package com.xul.yygh.user.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xul.yygh.cmn.client.DictFeignClient;
import com.xul.yygh.user.mapper.PatientMapper;
import com.xul.yygh.user.service.PatientService;
import model.com.xul.yygh.enums.DictEnum;
import model.com.xul.yygh.model.user.Patient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xul
 * @create 2021-07-15 20:41
 */

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * 获取就诊人的列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<Patient> findAllByUserId(Long userId) {
        // 根据userId查询所有就诊人信息列表
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);
        // 通过远程调用 得到编码对应的具体内容 查询数据字典表的内容
        patientList.stream().forEach(item -> {
            // 其他参数的封装
            this.packPatientList(item);
        });
        return patientList;
    }

    /**
     * 根据id获取就诊人的信息
     *
     * @param id
     * @return
     */
    @Override
    public Patient getPatientById(Long id) {
        return this.packPatientList(baseMapper.selectById(id));
    }

    /**
     * patientList的其他参数的封装
     *
     * @param patient
     * @return
     */
    private Patient packPatientList(Patient patient) {
        // 根据证件类型编码 获取证件类型具体值
        String CertificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        //联系人证件类型
        String contactsCertificatesTypeString =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
        //省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());

        patient.getParam().put("certificatesTypeString", CertificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());

        return patient;
    }
}
